<template>
  <div>
    <div class="up_box">
      <template>
        <van-uploader v-model="yyzzData" :max-count="1" />
      </template>
    </div>
    <div class="qymc">{{ qymc }}</div>
    <div class="tyshxydm">{{ tyshxydm }}</div>
    <div class="sc_text" v-if="!qymc">点击上传营业执照</div>
    <div class="go_sub">
      <van-button type="info" size="large" @click="gosubmit">提交</van-button>
    </div>
  </div>
</template>
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
import {
  businessLicenseRecognition,
  idCardCompanyInfoAdd,
} from "@/interfaces/authorize";
export default {
  name: "" /*1. Vue扩展 */,
  extends: "", // extends和mixins都扩展逻辑，需要重点放前面
  mixins: [],
  components: {} /* 2. Vue数据 */,
  props: {},
  model: { prop: "", event: "" }, // model 会使用到 props
  data() {
    return {
      yyzzData: [],
      tempFilePath: "",
      qymc: "",
      tyshxydm: "",
    };
  },
  computed: {},
  watch: {
    yyzzData(newval, oldval) {
      console.log(newval, "我看看怎个回事1");
      if (newval.length == 0) {
        this.qymc = "";
        this.tyshxydm = "";
      } else {
        this.getBusiness(newval);
      }
    },
  } /* 3. Vue资源 */, // watch 监控的是 props 和 data，有必要时监控computed
  filters: {},
  directives: {} /* 4. Vue生命周期 */,
  created() {},
  mounted() {},
  destroy() {} /* 5. Vue方法 */,
  methods: {
    async gosubmit() {
      if (this.qymc) {
        let userId = this.getUrlParam("userId");
        let parmas = {
          source: "镇江市",
          userId: userId,
          idCardCompanyInfoList: [
            {
              tyshxydm: this.tyshxydm,
              qymc: this.qymc,
            },
          ],
        };
        console.log(parmas, "parmasparmas");
        const res = await idCardCompanyInfoAdd(parmas);
        console.log(res);
        if (res.code == 200) {
          this.$toast(res.msg);
          setTimeout(function () {
            wx.miniProgram.navigateBack({ delta: 1});
          }, 1000);
        }
      } else {
        this.$toast("请上传有效营业执照");
      }
    },
    getBusiness(data) {
      console.log(data, "拿到了");
      const info = new FormData();
      info.append("file", data[0].file);
      console.log(data[0].file, info, "infoinfo");
      businessLicenseRecognition(info).then((res) => {
        console.log(res, "调接口了");
        this.qymc = res.data.qymc;
        this.tyshxydm = res.data.tyshxydm;
      });
    },
    getUrlParam(name) {
      const queryStr = String(window.location).split("?")[1] || "";
      name = encodeURIComponent(name);
      let arr = new RegExp("(^|&)" + name + "=([^&]*)(&|$)").exec(queryStr);
      if (arr) {
        return decodeURIComponent(RegExp.$2);
      } else {
        return "";
      }
    },
  }, // all the methods should be put here in the last
};
</script>
<style scoped lang="scss">
.up_box {
  text-align: center;
  //   background: red;
  width: 250px;
  height: 250px;
  margin: auto;
  margin-top: 130px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid #000;
}
.go_sub {
  width: 80%;
  margin: auto;
  margin-top: 50px;
}
.sc_text {
  text-align: center;
  margin: auto;
  margin-top: 40px;
}
.qymc {
  text-align: center;
  margin-top: 25px;
}
.tyshxydm {
  text-align: center;
  margin-top: 12px;
}
/deep/.van-uploader__upload {
  width: 250px;
  height: 250px;
  margin: auto;
}
/deep/.van-uploader__preview {
  margin: 0;
}
/deep/.van-uploader__preview-image {
  width: 230px;
  height: 230px;
}
</style>
